//
//  MoreTableViewController.m
//  Custom_Weibo
//
//  Created by LEO on 12/10/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "MoreTableViewController.h"
#import "SDImageCache.h"
#import "AppDelegate.h"

@interface MoreTableViewController()<UIAlertViewDelegate, UITableViewDataSource> {
    __weak IBOutlet ThemeImageView *themeImage;
    __weak IBOutlet ThemeImageView *cleanImage;
    __weak IBOutlet ThemeImageView *adviceImage;
    __weak IBOutlet ThemeImageView *aboutImage;
    
    __weak IBOutlet ThemeLabel *themeLabel;
    __weak IBOutlet ThemeLabel *cleanLabel;
    __weak IBOutlet ThemeLabel *adviceLabel;
    __weak IBOutlet ThemeLabel *aboutLabel;
    __weak IBOutlet ThemeLabel *logLabel;
    
    __weak IBOutlet ThemeLabel *themeDetailLabel;
    __weak IBOutlet ThemeLabel *cleanDetailLabel;
}

@end

@implementation MoreTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    themeImage.imageName = @"more_icon_theme";
    cleanImage.imageName = @"more_icon_feedback";
    adviceImage.imageName = @"more_icon_draft";
    aboutImage.imageName = @"more_icon_account";
    
    themeLabel.colorName = @"More_Item_Text_color";
    cleanLabel.colorName = @"More_Item_Text_color";
    adviceLabel.colorName = @"More_Item_Text_color";
    aboutLabel.colorName = @"More_Item_Text_color";
    logLabel.colorName = @"More_Item_Text_color";
    
    themeDetailLabel.colorName = @"More_Item_Text_color";
    cleanDetailLabel.colorName = @"More_Item_Text_color";
    
    self.tableView.dataSource = self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.tableView.backgroundColor = [[ThemeManager defaultManager] getThemeColor:@"More_Item_color"];
    self.tableView.separatorColor = [[ThemeManager defaultManager] getThemeColor:@"More_Item_Line_color"];
    
    //  After changing the theme, change the theme label as well.
    themeDetailLabel.text = [ThemeManager defaultManager].themeName;
    
    //  Before the view did load, calculating the size of cache.
    [self countCache];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)countCache {
    //  Calculating the cache size.
    NSUInteger cacheSize = [[SDImageCache sharedImageCache] getSize];
    if (cacheSize / 1024.0 / 1024.0 < 1.0) {
        cleanDetailLabel.text = [NSString stringWithFormat:@"%luKB", cacheSize / 1024];
    }else {
    cleanDetailLabel.text = [NSString stringWithFormat:@"%.1fM", cacheSize / 1024.0 / 1024.0];
    }
}

#pragma mark - TableView DataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //  Clean cache.
    if (indexPath.section == 0 && indexPath.row == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"操作警告" message:@"清除缓存后, 不可撤销" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.delegate = self;
        [alert show];
    }
    
    //  Deselected the cell.
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Alert Action
//  When pressed yes, clear the disk.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[SDImageCache sharedImageCache] clearDisk];
        [self countCache];
    }
}

@end
